package creationalPatterns.builder;

class Car {
    private String brand;
    private String model;
    private String color;

    public Car() {
    }

    public Car(String brand, String model, String color) {
        this.brand = brand;
        this.model = model;
        this.color = color;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", model='" + model + '\'' +
                ", color='" + color + '\'' +
                '}';
    }
}

abstract class Builder {
    protected Car car;

    public Car getCar() {
        return car;
    }

    public void build() {
        car = new Car();
    }

    public abstract void buildBrand();

    public abstract void buildModel();

    public abstract void buildColor();
}

class BuilderImplA extends Builder {
    public BuilderImplA() {
        build();
    }

    @Override
    public void buildBrand() {
        car.setBrand("Toyota");
    }

    @Override
    public void buildModel() {
        car.setModel("Corolla");
    }

    @Override
    public void buildColor() {
        car.setColor("Red");
    }
}

class BuilderImplB extends Builder {
    public BuilderImplB() {
        build();
    }

    @Override
    public void buildBrand() {
        car.setBrand("Honda");
    }

    @Override
    public void buildModel() {
        car.setModel("Civic");
    }

    @Override
    public void buildColor() {
        car.setColor("Blue");
    }
}

class Director {
    private Builder builder;

    public Director(Builder builder) {
        this.builder = builder;
    }

    public void setBuilder(Builder builder) {
        this.builder = builder;
    }

    public Car construct() {
        builder.buildBrand();
        builder.buildModel();
        builder.buildColor();
        return builder.getCar();
    }
}

public class BuilderPattern {
    public static void main(String[] args) {
        Builder builderA = new BuilderImplA();
        Director director = new Director(builderA);
        System.out.println(director.construct());
        Builder builderB = new BuilderImplB();
        director.setBuilder(builderB);
        System.out.println(director.construct());
    }
}
